package com.ruix.app.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.facebook.drawee.view.SimpleDraweeView;
import com.ruix.app.R;
import com.ruix.app.model.PostAuther;
import com.ruix.app.model.PostsCard;

/**
 * Created by hadexs on 2017/5/12.
 */

public class PostAuthorInfoDialog extends Dialog implements View.OnClickListener{

    private PostsCard postsCard;
    private PostAuther postAuther;
    private SimpleDraweeView autherAvatar;
    private TextView autherName;
    private TextView autherCompany;
    private TextView autherPersition;
    private TextView autherLocation;

    public PostAuthorInfoDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
    }

    public PostAuthorInfoDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_post_auther_info);
        setCanceledOnTouchOutside(true);
        //初始化界面控件
        //初始化界面控件的事件
        initEvent();
        Window dialogWindow = getWindow();

        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.gravity = Gravity.CENTER;
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(lp);
    }

    private void initEvent() {
        findViewById(R.id.iv_page_close).setOnClickListener(this);
         autherAvatar = (SimpleDraweeView) findViewById(R.id.iv_post_auther_avatar);
         autherName = (TextView) findViewById(R.id.tv_post_auther_name);
         autherCompany = (TextView) findViewById(R.id.tv_auther_company);
         autherPersition = (TextView) findViewById(R.id.tv_auther_persition);
         autherLocation = (TextView) findViewById(R.id.tv_auther_location);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_page_close:
                dismiss();
                break;
        }
    }



    private void updateView(){
        if (postAuther != null) {
            autherName.setText(postAuther.getName());
            if (postAuther.getSex() == 0) {
                Drawable drawable = getContext().getResources().getDrawable(R.drawable.auther_gender_female);
                drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
                autherName.setCompoundDrawables(null, null, drawable, null);
            } else {
                Drawable drawable = getContext().getResources().getDrawable(R.drawable.auther_gender_male);
                drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
                autherName.setCompoundDrawables(null, null, drawable, null);
            }
            autherCompany.setText("公司："+postAuther.getCompany());
            autherLocation.setText("地址："+postAuther.getLocate());
            autherPersition.setText("职位："+postAuther.getPosition());
            autherAvatar.setImageURI(postAuther.getAvatar());
        }
    }


    public void setPostAuther(PostAuther postAuther) {
        this.postAuther = postAuther;
        updateView();
    }

    public void show(PostAuther postAuther){
        if (postAuther != null) {
            this.postAuther = postAuther;
//            autherAvatar.setImageURI(item.getAvatarUrl());
//            autherName.setText(item.getUserName());
//            updateView();
            show();
        }
    }


}
